package com.hellohoper.script.config.starter;

import com.hellohoper.script.config.engine.container.EngineContainer;
import com.hellohoper.script.config.engine.container.GroovyEngineContainer;
import com.hellohoper.script.config.engine.container.GroovyScriptContainer;
import com.hellohoper.script.config.engine.container.ScriptContainer;
import com.hellohoper.script.config.engine.container.file.ScriptFileOptionService;
import com.hellohoper.script.config.engine.engine.EngineService;
import com.hellohoper.script.config.engine.engine.GroovyEngineService;
import com.hellohoper.script.config.engine.run.GroovyScriptRunnerService;
import com.hellohoper.script.config.engine.run.ScriptRunnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * 配置
 * Created by hk on 12/23/16.
 */
public class GroovyConfiguration {

    @Autowired
    private Environment environment;

    /**
     * 脚本容器
     */
    @Bean
    public ScriptContainer scriptContainer() {
        return new GroovyScriptContainer();
    }

    /**
     * 引擎容器
     */
    @Bean
    public EngineContainer engineContainer() {
        return new GroovyEngineContainer();
    }

    /**
     * 引擎服务
     */
    @Bean
    public EngineService engineService(ScriptContainer scriptContainer, EngineContainer engineContainer) {
        GroovyEngineService groovyEngineService = new GroovyEngineService();
        groovyEngineService.setEngineContainer(engineContainer);
        groovyEngineService.setScriptContainer(scriptContainer);
        return groovyEngineService;
    }

    /**
     * 引擎执行服务
     */
    @Bean
    public ScriptRunnerService scriptRunnerService(EngineService engineService) {
        GroovyScriptRunnerService groovyScriptRunnerService = new GroovyScriptRunnerService();
        groovyScriptRunnerService.setEngineService(engineService);
        groovyScriptRunnerService.setAppName(environment.getProperty("spring.application.name"));
        return groovyScriptRunnerService;
    }

    /**
     * 引擎文件服务
     */
    @Bean
    public ScriptFileOptionService scriptFileOptionService() {
        ScriptFileOptionService fileOptionService = new ScriptFileOptionService();
        return fileOptionService;
    }

}
